package org.osomit.sacct.account.registry.impl;

import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertEquals;
import org.osomit.sacct.account.registry.iface.AccountRegistry;
import org.osomit.sacct.account.registry.iface.AccountRegistryLoader;
import org.osomit.sacct.entity.ApplicationAccount;
import org.osomit.sacct.server.config.impl.ServerConfigImpl;
import org.osomit.sacct.server.config.iface.ServerConfig;

import java.util.List;

/**
 * @author Jian Fang (John.Jian.Fang@gmail.com)
 *
 * Date: Jun 30, 2009
 */
public class FileAccountRegistryLoader_UT {
    private static AccountRegistry registry = new InMemoryAccountRegistry();
    private static AccountRegistryLoader loader = new FileAccountRegistryLoader();

    @BeforeClass
    public static void setup(){
        ApplicationAccount acnt1 = new ApplicationAccount();
        acnt1.setName("cust_accnt");
        acnt1.setPassword("webcontent");
        ApplicationAccount acnt2 = new ApplicationAccount();
        acnt2.setName("production_app");
        acnt2.setPassword("PwD4ProdApp");
        ApplicationAccount acnt3 = new ApplicationAccount();
        acnt3.setName("johnjianfang@gmail.com");
        acnt3.setPassword("P@ssword");
        registry.addAccount("server", acnt1);
        registry.addAccount("server", acnt2);
        registry.addAccount("jianfang", acnt3);
        ServerConfig config = new ServerConfigImpl();
        config.setAccountFileName("target/test.reg");
        ((FileAccountRegistryLoader)loader).setConfig(config);
    }

    @Test
    public void testPersistAndLoad(){
        loader.persist(registry, "GO0d4You");
        registry.clear();
        loader.load(registry, "GO0d4You");
        List<ApplicationAccount> accounts = registry.getAllAccounts("server");
        assertNotNull(accounts);
        assertEquals(2, accounts.size());
        accounts = registry.getAllAccounts("jianfang");
        assertNotNull(accounts);
        assertEquals(1, accounts.size());
        assertEquals("johnjianfang@gmail.com", accounts.get(0).getName());
        assertEquals("P@ssword", accounts.get(0).getPassword());
    }
}
